package com.RedRiver.hospitalsystem.personal_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.personal_service.common.Personal_serviceResultCode;
import com.RedRiver.hospitalsystem.personal_service.common.exception.WorkerException;
import com.RedRiver.hospitalsystem.personal_service.infra.mapper.WorkerMapper;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Doctor;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Worker;
import com.RedRiver.hospitalsystem.personal_service.service.Form.*;
import com.RedRiver.hospitalsystem.personal_service.service.WorkerService;
import com.RedRiver.hospitalsystem.personal_service.service.info.DoctorPageInfo;
import com.RedRiver.hospitalsystem.personal_service.service.info.WorkerPageInfo;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class WorkerServiceImpl extends ServiceImpl<WorkerMapper, Worker> implements WorkerService {

    @Resource
    private WorkerMapper workerMapper;

    /**
     * 分页查询所有工作人员
     * @param form
     * @return
     */
    @Override
    public IPage<WorkerPageInfo> findAllWorker(PageForm form) {
        Page<Worker> workerInfoPage = new Page<>();
        workerInfoPage = this.baseMapper.selectPage(new Page<>(form.getPage() == 0 ? 1 : form.getPage(),
                                                    form.getPageSize() == 0 ? 2 : form.getPageSize()),null);
        List<Worker> workerInfoList = workerInfoPage.getRecords();
        List<WorkerPageInfo> workerPageInfoList = BeanUtil.copyToList(workerInfoList, WorkerPageInfo.class);
        Page<WorkerPageInfo> page = new Page<>();
        page.setRecords(workerPageInfoList);
        page.setCurrent(workerInfoPage.getCurrent());
        page.setSize(workerInfoPage.getSize());
        page.setTotal(workerInfoPage.getTotal());
        return page;
    }

    /**
     * 根据部门id分页查询该部门下所有工作员工
     * @param form
     * @return
     */
    @Override
    public IPage<WorkerPageInfo> findAllWorkerByDepartmentId(WorkerPageForm form) {
        Page<Worker> workerInfoPage = new Page<>();
        workerInfoPage = this.baseMapper.selectPage(new Page<>(form.getPage() == 0 ? 1 : form.getPage(),
                form.getPageSize() == 0 ? 2 : form.getPageSize()),new QueryWrapper<Worker>().eq("department_id",form.getDepartmentId()));
        List<Worker> workerInfoList = workerInfoPage.getRecords();
        List<WorkerPageInfo> workerPageInfoList = BeanUtil.copyToList(workerInfoList, WorkerPageInfo.class);
        Page<WorkerPageInfo> page = new Page<>();
        page.setRecords(workerPageInfoList);
        page.setCurrent(workerInfoPage.getCurrent());
        page.setSize(workerInfoPage.getSize());
        page.setTotal(workerInfoPage.getTotal());
        return page;
    }

    /**
     * 根据性别分页查询该性别的所有工作人员
     * @param form
     * @return
     */
    @Override
    public IPage<WorkerPageInfo> findAllWorkerByWorkerSex(WorkerSexPageForm form) {
        Page<Worker> workerInfoPage = new Page<>();
        workerInfoPage = this.baseMapper.selectPage(new Page<>(form.getPage() == 0 ? 1 : form.getPage(),
                form.getPageSize() == 0 ? 2 : form.getPageSize()),new QueryWrapper<Worker>().eq("worker_sex",form.getWorkerSex()));
        List<Worker> workerInfoList = workerInfoPage.getRecords();
        List<WorkerPageInfo> workerPageInfoList = BeanUtil.copyToList(workerInfoList, WorkerPageInfo.class);
        Page<WorkerPageInfo> page = new Page<>();
        page.setRecords(workerPageInfoList);
        page.setCurrent(workerInfoPage.getCurrent());
        page.setSize(workerInfoPage.getSize());
        page.setTotal(workerInfoPage.getTotal());
        return page;
    }

    /**
     * 根据姓名分页查询所有工作人员
     * @param form
     * @return
     */
    @Override
    public IPage<WorkerPageInfo> findPageAllWorkerByWorkerName(WorkerNamePageForm form) {
        Page<Worker> workerInfoPage = new Page<>();
        workerInfoPage = this.baseMapper.selectPage(new Page<>(form.getPage() == 0 ? 1 : form.getPage(),
                form.getPageSize() == 0 ? 2 : form.getPageSize()),new QueryWrapper<Worker>().eq("worker_name",form.getWorkerName()));
        List<Worker> workerInfoList = workerInfoPage.getRecords();
        List<WorkerPageInfo> workerPageInfoList = BeanUtil.copyToList(workerInfoList, WorkerPageInfo.class);
        Page<WorkerPageInfo> page = new Page<>();
        page.setRecords(workerPageInfoList);
        page.setCurrent(workerInfoPage.getCurrent());
        page.setSize(workerInfoPage.getSize());
        page.setTotal(workerInfoPage.getTotal());
        return page;
    }

    /**
     * 新增工作人员
     * @param workerForm
     * @return
     */
    @Override
    public Result addWorker(WorkerForm workerForm) {
        List<Worker> workers = workerMapper.selectList(new QueryWrapper<Worker>().eq("worker_card", workerForm.getWorkerCard()));
        for (Worker worker : workers) {
            if (!worker.getWorkerName().equals(workerForm.getWorkerName()))throw new WorkerException(Personal_serviceResultCode.WORKER_CARD_EXISTED);
            if (worker.getWorkerName().equals(workerForm.getWorkerName())&&worker.getWorkerSex()== workerForm.getWorkerSex()&&
                worker.getWokerAddress().equals(workerForm.getWokerAddress())&&worker.getDepartmentId().equals(workerForm.getDepartmentId()))
                throw new WorkerException(Personal_serviceResultCode.WORKER_EXISTED);
        }
        int insert = workerMapper.insert(BeanUtil.copyProperties(workerForm, Worker.class));
        if (insert==0) throw new WorkerException(Personal_serviceResultCode.WORKER_ADD_ERROR);
        return Result.getInstance().setMsg("新增成功");
    }

    /**
     * 修改工作人员信息
     * @param newUpdateWorker
     * @return
     */
    @Override
    public Result updateNewWorker(NewUpdateWorker newUpdateWorker) {
        Worker worker_id = workerMapper.selectOne(new QueryWrapper<Worker>().eq("worker_id", newUpdateWorker.getWorkerId()));
        if (worker_id==null) throw new WorkerException(Personal_serviceResultCode.WORKER_NOT_EXISTED);
        List<Worker> workers = workerMapper.selectList(new QueryWrapper<Worker>().eq("worker_card", newUpdateWorker.getWorkerCard()));
        if (workers!=null) for (Worker worker : workers) {
            if (worker.getWorkerId()!=newUpdateWorker.getWorkerId()) throw new WorkerException(Personal_serviceResultCode.WORKER_CARD_EXISTED);
        }
        Worker entity = BeanUtil.copyProperties(newUpdateWorker, Worker.class);
        int i = workerMapper.update(entity, new QueryWrapper<Worker>().eq("worker_id", entity.getWorkerId()));
        if (i==0) throw new WorkerException(Personal_serviceResultCode.WORKER_UPDATE_ERROR);
        return Result.getInstance().setMsg("修改成功");
    }

    /**
     * 根据id删除工作人员信息
     * @param workerId
     * @return
     */
    @Override
    public Result deleteWorkerById(Integer workerId) {
        Worker worker_id = workerMapper.selectOne(new QueryWrapper<Worker>().eq("worker_id", workerId));
        if (worker_id==null) throw new WorkerException(Personal_serviceResultCode.WORKER_NOT_EXISTED);
        int i = workerMapper.delete(new QueryWrapper<Worker>().eq("worker_id", workerId));
        if (i==0) throw new WorkerException(Personal_serviceResultCode.WORKER_DELETE_ERROR);
        return Result.getInstance().setMsg("删除成功");
    }


}
